/**
 * File    : UserServiceImpl.java
 * Created : 2007-12-20
 * By      : lhg
 */
package com.luhuiguo.sso.tutorial.services;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UsernameNotFoundException;
import org.springframework.transaction.annotation.Transactional;

import com.luhuiguo.sso.tutorial.dao.RoleDao;
import com.luhuiguo.sso.tutorial.dao.UserDao;
import com.luhuiguo.sso.tutorial.entity.Role;
import com.luhuiguo.sso.tutorial.entity.User;

/**
 * @author lhg
 * @version $Id: UserServiceImpl.java 2 2008-10-04 13:04:21Z LuHuiguo $
 * 
 */
@Transactional
public class UserServiceImpl implements UserService {

	private UserDao userDao;

	private RoleDao roleDao;

	/**
	 * @param userDao
	 *            the userDao to set
	 */
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	/**
	 * @param roleDao
	 *            the roleDao to set
	 */
	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {
		User u = userDao.findById(username);
		if (u == null) {
			throw new UsernameNotFoundException(username + " not found");
		}
		return u;
	}

	@Transactional(readOnly = true)
	public User getUser(String id) {
		return userDao.findById(id);
	}

	@Transactional(readOnly = true)
	public List<User> getAllUser(int... rowStartIdxAndCount) {
		return userDao.findAll(rowStartIdxAndCount);
	}

	@Transactional(readOnly = true)
	public Role getRole(String id) {
		return roleDao.findById(id);
	}

	@Transactional(readOnly = true)
	public List<Role> getAllRole(int... rowStartIdxAndCount) {
		return roleDao.findAll(rowStartIdxAndCount);
	}

	public void createUser(User user) {
		userDao.create(user);

	}

	public void updateUser(User user) {
		userDao.update(user);
	}

	public void deleteUser(String id) {
		userDao.delete(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.luhuiguo.sso.sample.tapestry.services.UserService#createRole(com.
	 * hundsun .openid.tutorial.entity.Role)
	 */
	public void createRole(Role role) {
		roleDao.create(role);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.luhuiguo.sso.sample.tapestry.services.UserService#deleteRole(java
	 * .lang .String)
	 */
	public void deleteRole(String id) {
		roleDao.delete(id);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.luhuiguo.sso.sample.tapestry.services.UserService#updateRole(com.
	 * hundsun .openid.tutorial.entity.Role)
	 */
	public void updateRole(Role role) {
		roleDao.update(role);

	}

}
